﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using MoneyLover.WCFMoneyLover;

namespace MoneyLover.Pages
{
    public partial class KeHoach : PhoneApplicationPage
    {
        public KeHoach()
        {
            InitializeComponent();
        }
        MoneyLoverServiceClient _db = new MoneyLoverServiceClient();
        private void btnthem_Click(object sender, RoutedEventArgs e)
        {
            lstkh.Visibility = Visibility.Collapsed;
            themkh.Visibility = Visibility.Visible;
        }

        private void btnluu_Click(object sender, RoutedEventArgs e)
        {
            lstkh.Visibility = Visibility.Visible;
            themkh.Visibility = Visibility.Collapsed;
            KEHOACH kh = new KEHOACH(); ;
            if (String.IsNullOrEmpty(txttenkehoach.Text.Trim()))
            {
                MessageBox.Show("Tên kế hoạch không hợp lệ");
            }
            kh.TENKH = txttenkehoach.Text.Trim();
            _db.InsertKeHoachCompleted += _db_InsertKeHoachCompleted;
            _db.InsertKeHoachAsync(kh);
        }

        void _db_InsertKeHoachCompleted(object sender, InsertKeHoachCompletedEventArgs e)
        {
            if (e.Result==false)
            {
                MessageBox.Show("Thêm kế hoạch thành công");
            }
            else
            {
                Loadreference();
            }
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            Loadreference();
            lstkh.Visibility = Visibility.Visible;
            themkh.Visibility = Visibility.Collapsed;
        }

        private void Loadreference()
        {
            _db.GetListKeHoachCompleted += _db_GetListKeHoachCompleted;
            _db.GetListKeHoachAsync();
        }

        void _db_GetListKeHoachCompleted(object sender, GetListKeHoachCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                lstlistkehoach.ItemsSource = e.Result;
            }
        }
    }
}